package com.etc.controller;

import com.etc.entity.Comments;
import com.etc.entity.Comments2;
import com.etc.entity.User;
import com.etc.service.CommentsService;
import com.etc.util.Layui;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Type;
import java.util.List;

@Controller
@RequestMapping("/comments")
public class CommentsController {

    Logger logger=Logger.getLogger(CommentsController.class);
    @Autowired
    CommentsService service;

    /**
     * 一级所有评论查询
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/allCom")
    @ResponseBody
    public Layui AllCom(@RequestParam(value = "page",defaultValue = "1")int page,
                        @RequestParam(value = "limit",defaultValue = "10")int limit,
                        @RequestParam(value = "comId",defaultValue = "")String comId,
                        @RequestParam(value = "userName",defaultValue = "")String userName,
                        @RequestParam(value = "comText",defaultValue = "")String comText){
        PageInfo<Comments> pageInfo=service.getAllComs(page, limit,comId,userName,comText);
        return Layui.data(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 删除一级评论
     * @param comId
     * @return
     */
    @RequestMapping("/delCom")
    @ResponseBody
    public ModelAndView delCom(String comId){
        ModelAndView mv=new ModelAndView();
        boolean flag=service.getDelCom(comId);
        if(flag){
            logger.debug("删除成功");
            mv.setViewName("back/list");
        }else{
            logger.debug("删除失败");
            mv.setViewName("back/list");
        }
        return mv;
    }

    /**
     * 二级所有评论查询
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/allCom2")
    @ResponseBody
    public Layui AllCom2(@RequestParam(value = "page",defaultValue = "1")int page,
                         @RequestParam(value = "limit",defaultValue = "10")int limit,
                         @RequestParam(value = "userName",defaultValue = "")String userName,
                         @RequestParam(value = "com2Text",defaultValue = "")String com2Text){
        PageInfo<Comments2> pageInfo=service.getAllComs2(page, limit,userName,com2Text);
        return Layui.data(pageInfo.getTotal(),pageInfo.getList());
    }
    /**
     * 删除二级评论
     * @param com2Id
     * @return
     */
    @RequestMapping("/delCom2")
    @ResponseBody
    public ModelAndView delCom2(String com2Id){
        ModelAndView mv=new ModelAndView();
        boolean flag=service.getDelComs2(com2Id);
        if(flag){
            logger.debug("删除成功");
            mv.setViewName("back/replys");
        }else{
            logger.debug("删除失败");
            mv.setViewName("back/replys");
        }
        return mv;
    }

    /**
     * 查询房间评论
     * @return
     */
    @RequestMapping("/queryRtComment")
    @ResponseBody
    public List<Comments> queryRtComment(){
        List<Comments> list = service.queryComByrtId();
        return list;
    }

    /**
     * 添加评论
     * @return
     */
    @RequestMapping("/addCom")
    @ResponseBody
    public List<Comments> addCom(String comments,String userName){
        Gson gson = new Gson();
        Comments com = gson.fromJson(comments, Comments.class);
        User user = new User(userName);
        com.setUser(user);
        logger.debug(com);
        service.addCom(com);
        return service.queryComByrtId();
    }

    /**
     * 添加评论
     * @return
     */
    @RequestMapping("/addCom2")
    @ResponseBody
    public List<Comments> addCom2(String comments2,String comId,String userName){
        Gson gson = new Gson();
        Comments2 com = gson.fromJson(comments2, Comments2.class);
        Comments comm = new Comments();
        comm.setComId(comId);
        User user = new User(userName);
        com.setUser(user);
        com.setComments(comm);
        logger.debug(com);
        service.addCom2(com);
        return service.queryComByrtId();
    }

    /**
     * 查询前三条房间评价
     * @return
     */
    @RequestMapping("/getUserCom")
    @ResponseBody
    public List<Comments> getUserCom(){
        List<Comments> list = service.getUserCom();
        return list;
    }
}
